import { routerRedux } from 'dva/router'
import { parse } from 'qs'
import config from 'config'
import { EnumRoleType } from 'enums'
import * as menuService from 'services/menu'
import { userLis, searchs, infoDetais, userLists, sends } from 'services/search'
import { type, group } from 'services/newfile'

const { prefix } = config

export default {
	namespace: 'fileTransfer',
	state: {
		list: '',
		visible: false,
		details: '',
		department2: '',
		depUser: '',
		infoData: '',
		params: '',
		pagination1: {
			showSizeChanger: true,
			showQuickJumper: true,
			showTotal: total => `共 ${total} 条`,
			current: 1,
			total: null
		},
	},
	subscriptions: {
		setup({ dispatch, history }) {
			// history.listen(location => {
			// 	const { query, pathname } = location;
			// 	if (pathname === "/file/fileTransfer") {
			// 		dispatch({
			// 			type: "searchList",
			// 			payload: {

			// 			}
			// 		});

			// 	}
			// });
		}
	},
	effects: {
		*reload(action, { put, select }) {
			const page = yield select(({ fileTransfer }) => fileTransfer.pagination1)

			yield put({
				type: 'searchList', payload: {
					current: page.current
				}
			});
		},
		*update({ payload }, { call, put }) {
			yield put({ type: 'hideModal' })
			yield put({ type: 'showLoading' })
			//	      const id = yield select(({ contract }) => contract.currentItem.id)
			//	      const newContract = { ...payload, id }
			const data = yield call(sends, payload)
			if (data) {
				yield put({ type: 'reload' });
			} else {
				yield put({ type: 'hideLoading' })
			}
		},
		*dep({ payload }, { call, put }) {
			const data = yield call(group, payload)
			if (data.success) {
				yield put({
					type: "querySuccessA",
					payload: {
						department2: data.list
					}
				})
			}
		},
		*users({ payload }, { call, put }) {
			const data = yield call(userLists, payload)
			if (data) {
				yield put({
					type: "querySuccessB",
					payload: {
						depUser: data.userList
					}
				})
			}
		},
		*showDet({ payload }, { call, put }) {
			const data = yield call(infoDetais, payload)
			yield put({
				type: 'showDetailSuccess',
				payload: {
					infoData: data
				}
			})
		},
		*onModalOk({ payload }, { call, put }) {
			yield put({ type: 'showLoading' })
			yield put({
				type: 'querySuccess',
				payload: {
					visible: false
				}
			})
		},
		*showModal({ payload }, { call, put }) {
			yield put({
				type: 'showModalSuccess',
				payload: {
					visible: true
				}
			})
		},
		*onCancels({ payload }, { call, put }) {
			yield put({
				type: 'showModalSuccess',
				payload: {
					visible: false
				}
			})
		},
		*searchList({ payload }, { call, put, select }) {
			const data = yield call(searchs, payload)
			//0为申请 1为审批
			if (data.success) {
				yield put({
					type: 'querySuccess3',
					payload: {
						params: payload,
						list: data.page.list,
						pagination1: {
							total: data.page.totalCount,
							current: data.page.currPage
						}
					}
				})
			}
		},
	},
	reducers: {
		showModalSuccess(state, action) {
			const { visible } = action.payload
			return {
				...state,
				visible
			}
		},
		querySuccess(state, action) {
			const { visible } = action.payload
			return {
				...state,
				visible
			}
		},
		querySuccess3(state, action) {
			const { list, pagination1, params } = action.payload
			return {
				...state,
				list,
				params,
				pagination1: {
					...state.pagination1,
					...pagination1
				},
			}
		},
		showDetailSuccess(state, action) {
			const { infoData } = action.payload
			return {
				...state,
				infoData
			}
		},
		querySuccessA(state, action) {
			const { department2 } = action.payload
			return {
				...state,
				department2
			}
		},
		querySuccessB(state, action) {
			const { depUser } = action.payload
			return {
				...state,
				depUser
			}
		},
		showLoading(state) {
			return { ...state, loading: true }
		},
		hideLoading(state) {
			return { ...state, loading: false }
		},
		hideModal(state, action) {
			return {
				...state,
				...action.payload,
				visible: false
			}
		},
	}
}
